package com.syni.mdd.yhd.common.all.config.mvc;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.syni.mdd.sjb.common.component.utils.common.config.CoustemLocalDateTimeDeserializer;
import com.syni.mdd.sjb.common.component.utils.common.config.CustomDateSerializer;
import com.syni.mdd.sjb.common.component.utils.common.config.CustomLocalDateTimeSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @className CustomMvcConfig
 * @description TOO
 * @Author cfx
 * @DATE 2020/5/16 12:56
 * @VERSION 1.0
 **/
@Configuration
public class CustomMvcConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //设置允许跨域的路径
        registry.addMapping("/**")
                //设置允许跨域请求的域名
                .allowedOrigins("*")
                //是否允许证书 不再默认开启
                .allowCredentials(true)
                //设置允许的方法
                .allowedMethods("*")
                //跨域允许时间
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        for(HttpMessageConverter httpMessageConverter : converters){
            if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = (MappingJackson2HttpMessageConverter) httpMessageConverter;
                //mappingJackson2HttpMessageConverter.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
                mappingJackson2HttpMessageConverter.getObjectMapper().configure(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS,false);
                JavaTimeModule javaTimeModule = new JavaTimeModule();
                javaTimeModule.addSerializer(LocalDateTime.class,new CustomLocalDateTimeSerializer());
                javaTimeModule.addDeserializer(LocalDateTime.class,new CoustemLocalDateTimeDeserializer());
                javaTimeModule.addSerializer(Date.class,new CustomDateSerializer());
                mappingJackson2HttpMessageConverter.getObjectMapper().registerModule(javaTimeModule);
            }
        }
    }
}
